﻿using System;
using System.IO;
using System;
using System.Net.Http;
using System.Threading.Tasks;


public class FileStorageService
{
    private readonly string _storagePath;

    public FileStorageService()
    {
        // 定义存储路径（例如：当前程序目录下的 OfflineData 文件夹）
        _storagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "OfflineData");
        if (!Directory.Exists(_storagePath))
        {
            Directory.CreateDirectory(_storagePath);
        }
    }

    public void SaveOfflineData(string data)
    {
        // 生成唯一的文件名
        string fileName = $"data_{Guid.NewGuid()}.json";
        string filePath = Path.Combine(_storagePath, fileName);

        // 将数据保存为文件
        File.WriteAllText(filePath, data);
    }

    public string[] GetOfflineDataFiles()
    {
        // 获取所有离线数据文件
        return Directory.GetFiles(_storagePath, "*.json");
    }

    public string ReadFileContent(string filePath)
    {
        // 读取文件内容
        return File.ReadAllText(filePath);
    }

    public void DeleteFile(string filePath)
    {
        // 删除文件
        File.Delete(filePath);
    }

public class UploadService
{
    private readonly FileStorageService _fileStorageService;

    public UploadService()
    {
        _fileStorageService = new FileStorageService();
    }

    public async Task UploadDataAsync(string data)
    {
        if (IsOnline())
        {
            try
            {
                // 尝试上传数据
                await SendDataToServerAsync(data);
            }
            catch (Exception ex)
            {
                // 上传失败，保存到本地文件
                _fileStorageService.SaveOfflineData(data);
            }
        }
        else
        {
            // 离线状态，保存到本地文件
            _fileStorageService.SaveOfflineData(data);
        }
    }

    private async Task SendDataToServerAsync(string data)
    {
        using (var client = new HttpClient())
        {
            var content = new StringContent(data);
            var response = await client.PostAsync("https://yourserver.com/api/upload", content);
            response.EnsureSuccessStatusCode();
        }
    }

    private bool IsOnline()
    {
        // 检查网络状态
        return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
    }

    public async Task TryUploadOfflineDataAsync()
    {
        if (IsOnline())
        {
            var offlineFiles = _fileStorageService.GetOfflineDataFiles();
            foreach (var filePath in offlineFiles)
            {
                try
                {
                    // 读取文件内容并上传
                    string data = _fileStorageService.ReadFileContent(filePath);
                    await SendDataToServerAsync(data);

                    // 上传成功后删除文件
                    _fileStorageService.DeleteFile(filePath);
                }
                catch (Exception ex)
                {
                    // 上传失败，保留文件
                    Console.WriteLine("Failed to upload offline data: " + ex.Message);
                }
            }
        }
    }
}
}